from system.action import Action

class ScreenWrap(Action):
    def run(self, **args):
        if 'res' in args:
            res = args['res']
        else:
            resObj = self.object.searchFirst(lambda obj: obj.hasVar('res'))
            if resObj:
                res = resObj.getVar("res",(800,600))
            else:
                res = 800,600
        for time, dtime in self.enumerateTime(args.get("updateTime",0.1)):
            if self.object.hasVar("pos"):
                x,y = self.object.getVar("pos",(0,0))
                change = False
                if x < 0:
                    change = True
                    x += res[0]
                elif x > res[0]:
                    change = True
                    x -= res[0]
                if y < 0:
                    change = True
                    y += res[1]
                elif y > res[1]:
                    change = True
                    y -= res[1]
                if change:
                    self.object.setVar("pos",(x,y))
